// ignore_for_file: unused_local_variable

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/RecordModel.dart';
import 'package:time_manager/view/statistics/dict.dart';

class StatisticController extends GetxController {
  var isLoading = false.obs;
  var list = <NavItem>[].obs;
  var currantLevel = StatisticConfig.level[0].obs;

  @override
  void onInit() async {
    // list.assignAll(StatisticConfig.navList);
    updateNav();
    super.onInit();
  }

  updateLevel() {
    currantLevel.update((val) {
      currantLevel.value = StatisticConfig.level[1];
    });
  }

  Future<void> onRefresh() async {
    await updateNav();
  }

  updateNav() async {
    var tempList = StatisticConfig.navList;
    var temp = await DataBaseRecord().selectRecord();
    var params = {
      'dateTotal': 0,
      'comNum': 0,
      'unComNum': 0,
      "comDuration": 0,
      "unComDUration": 0,
      "dataDuration": 0
    };

    temp.forEach((it) {
      if (it.status == '2') {
        params['comNum'] = params['comNum']! + 1;
        params['comDuration'] = params['comDuration']! + int.parse(it.duration);
      } else {
        params['unComNum'] = params['unComNum']! + 1;
        params['unComDUration'] =
            params['unComDUration']! + int.parse(it.duration);
      }
    });
    params['dateTotal'] = params['comNum']! + params['unComNum']!;
    params['dataDuration'] = params['comDuration']! + params['unComDUration']!;
    list.clear();
    tempList.forEach((it) {
      it.num = params[it.attr];
    });
    int levellength = StatisticConfig.level.length;
    bool flag = false;
    for (int i = (levellength - 1); i > 0; i--) {
      if (!flag) {
        if (params['comDuration']! > StatisticConfig.level[i].num) {
          flag = true;
          currantLevel.value = StatisticConfig.level[i];
        }
      }
    }
    currantLevel.value.rate =
        ((params['comDuration']! / StatisticConfig.level[levellength - 1].num) *
                100)
            .toStringAsFixed(1);

    list.assignAll(tempList);
  }
}
